<?php
declare(strict_types=1);

namespace Modules\Admin\Service;

use DB;
use HiCommon\Repository\RepairstoreRepository;
use HiCommon\Repository\RepairstoreGoodsRepository;
use HiCommon\Repository\RepairstoreGoodsImgRepository;
use HiCommon\Repository\RepairstoreTypeRepository;
use HiCommon\Repository\AdminsRepository;

class RepairstoreService
{
    /**
     * 维修店详情
     */
    public function repairstoreDetail(array $where)
    {
        $detail = RepairstoreRepository::get_where_one($where);

        if ($detail) {
            return ['detail' => $detail->toarray()];
        }
        return ['detail' => []];
    }

    /***
     * 维修店管理员编辑
     */
    public function repairUpdate(array $data, $admin_id)
    {
        try {
            $data['cate'] = 2;
            $res = RepairstoreRepository::edit_or_create(['admin_id' => $admin_id], array_filter($data));

            if ($res && $res->id && $res->longitude && $res->latitude) {
                //更新门店对应的商品经纬度
                $map = ['repairstore_id' => $res->id];
                $option = ['longitude' => $res->longitude, 'latitude' => $res->latitude];
                RepairstoreGoodsRepository::edit_one_by_where((array)$map, (array)$option);
            }
            $arr_data = [
                'id' => $res['admin_id'],
                'phone' => $res['phone'],
                'username' => $res['name'],
            ];
            AdminsRepository::edit_one($arr_data);

            return hello_success('编辑成功', $res);
        } catch (Exception $e) {

            return hello_error('编辑失败');
        }
    }


    /**
     * 列表
     */
    public function getList($data)
    {
        $list = list_page_data(RepairstoreRepository::get_list($data));
        return $list;
    }


    /**
     * 删除
     */
    public function del($data)
    {
        return CheckpointRepository::delete_one((int)$data['id']);
    }

    /*******************************************维修站商品*************************************************/

    /**
     * 商品列表
     */
    public function getGoodsList($where)
    {
        $repair = RepairstoreRepository::get_where_one(['admin_id' => admin_user_id()]);
        if ($repair) {
            $where['repairstore_id'] = $repair->id;
        } else {
            return hello_error('账户不存在, 请先到管理中心创建账户');
        }
        $data = RepairstoreGoodsRepository::get_list($where);

        $data->appends($where);//添加链接参数
        $list = list_page_data($data, $where);
        $list['type'] = self::getGoodsType();

        return $list;
    }

    /**
     * 商品类型列表
     */
    public function getGoodsType()
    {
        return RepairstoreTypeRepository::get_all(['status' => 1], ['id', 'title']);
    }


    /**
     * 添加商品
     */

    public function addOne(array $arrInput)
    {
        try {
            unset($arrInput['file']);
            $repair = RepairstoreRepository::get_where_one(['admin_id' => admin_user_id()]);
            if ($repair) {
                $arrInput['repairstore_id'] = $repair->id;
                $arrInput['longitude'] = $repair->longitude;
                $arrInput['latitude'] = $repair->latitude;
            } else {
                return hello_error('账户不存在');
            }

            $imgs = $arrInput['img'];
            unset($arrInput['img']);

            $res = RepairstoreGoodsRepository::add_one($arrInput);

            $imgArray = array();
            foreach ($imgs as $key => $value) {
                $imgArray['goods_id'] = $res->id;
                $imgArray['picurl'] = $value;
                $imgArray['sorts'] = $key + 1;
                RepairstoreGoodsImgRepository::add_one($imgArray);//存入图片信息
            }

            if (empty($res)) {
                return hello_error('添加失败');
            }

            return hello_success('添加成功', $res);
        } catch (\Exception $exception) {
            return ['state' => 1, 'msg' => $exception->getMessage()];
        }
    }

    /**
     * 获取一条数据
     */
    public function getOne(int $id)
    {
        $data = RepairstoreGoodsRepository::get_one($id);
        if ($data) {
            return $data->toarray();
        }
        return [];
    }

    /**
     * 修改一条数据
     */
    public function editOne(array $data)
    {
        try {
            unset($data['file']);

            $imgs = $data['img'];
            unset($data['img']);

            $res = RepairstoreGoodsRepository::edit_one($data);

            foreach ($imgs as $key => &$value) {
                $value['goods_id'] = $data['id'];
                $value['sorts'] = $key + 1;

                RepairstoreGoodsImgRepository::edit_or_create(['id' => $value['id']], $value);//存入图片信息
            }


            if (empty($res)) {
                return hello_error('修改失败');
            }

            return hello_success('修改成功', $res);
        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 删除商品图片
     */
    public function delPic($data)
    {
        try {
            if ($data <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = RepairstoreGoodsImgRepository::delete_one($data);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');

        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }

    /**
     * 删除
     */
    public function delGoods($data)
    {
        try {
            if ($data <= 0) {
                return hello_error('数据id不能为空');
            }

            $res = RepairstoreGoodsRepository::delete_one($data);
            if ($res === false) {
                return hello_error('操作失败');
            }
            return hello_success('成功');

        } catch (\Exception $exception) {
            return hello_error('失败', [], $exception->getMessage());
        }
    }


}